package com.liyi.letter.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liyi.letter.common.config.MD5;
import com.liyi.letter.common.result.Result;
import com.liyi.letter.system.entity.Employee;
import com.liyi.letter.system.service.DepartmentService;
import com.liyi.letter.system.service.EmployeeService;
import com.liyi.letter.system.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @program: letter_parent
 * @description
 * @author: tianwen
 * @create: 2023-06-06 11:26
 **/
@RestController
@RequestMapping("/employee/system")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private OssService ossService;

    @Autowired
    private DepartmentService departmentService;


    @PostMapping("/login")
    public Result login(Employee employe) {
        Map token = new HashMap();
        token.put("token", "admin-token");
        return Result.ok(token);
    }

    @GetMapping(value = "/info")
    public Result info(String token) {
        Map map = new HashMap();
        map.put("roles", "[admin]");
        map.put("avatar", "https://img.soogif.com/NQy8FNhHUebORPnFRkJogMVARjCvkpRv.gif");
        map.put("name", "迪迦");
        return Result.ok(map);
    }

    /**
     * 上传头像
     *
     * @param file
     * @return
     */
    @PostMapping("addPhoto")
    public Result uploadOssFile(@RequestParam("file") MultipartFile file) {
        //获取上传文件  MultipartFile
        //返回上传到oss的路径
        String url = ossService.uploadFileAvatar(file);
        System.err.println(url);
        return Result.ok(url);
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping("/addEmployee")
    public Result addEmployee(@RequestBody Employee employee) throws Exception {
        boolean save = employeeService.addEmployee(employee);
        if (save){
            return Result.ok("新增员工成功");
        }
        return Result.fail();
    }

    /**
     * 根据条件分页查询
     *
     * @param page
     * @param pageSize
     * @param employee
     * @return
     */
    @PostMapping("/page/{page}/{pageSize}")
    public Result<Page> page( @RequestBody Employee employee, @PathVariable Integer page, @PathVariable Integer pageSize) {
        Page page1 = employeeService.MyPage(page, pageSize, employee);
        return Result.ok(page1);
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */

    @PutMapping("/update")
    public Result<String> updateEmployee(@RequestBody Employee employee) {
        employeeService.updateEmployee(employee);
        return Result.ok("员工信息修改成功");
    }

    /**
     * 根据员工id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result<Employee> getById(@PathVariable("id") Long id) throws Exception {

        Employee emp = employeeService.getById(id);
        if (emp != null) {
            emp.setPassword("xxxxxxxx");

            return Result.ok(emp);
        }
        return Result.fail();
    }

    /**
     * 根据员工id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deletedById/{id}")
    public Result<Employee> deletedEmployeeById(@PathVariable("id") Long id) {
        employeeService.deletedEmployeeById(id);
        return Result.ok();
    }
}
